package com.itranswarp.learnjava.dao;

import com.itranswarp.learnjava.bean.User;
import com.itranswarp.learnjava.utils.DBUtil;
import com.itranswarp.learnjava.bean.Role;
import com.itranswarp.learnjava.bean.PageResult;

import java.util.ArrayList;

import java.sql.*;
public class RoleDAO {
    public PageResult<Role> getAllRoles(int offset, int pageSize) {
        ArrayList<Role> roles = new ArrayList<>();
        PageResult<Role> result = new PageResult<>();

        try {
            Connection conn = DBUtil.getConnection();
            String sql = "SELECT * FROM roles LIMIT ?, ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, offset *pageSize);
            stmt.setInt(2, pageSize);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                roles.add(extractRow(rs));
            }
            rs.close();
            stmt.close();

            result.pageSize = pageSize;
            result.currentPage = offset + 1;
            result.records = roles;
            result.totalRecords = getRolesCount();
            result.totalPages = (int) Math.ceil((double) result.totalRecords / pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    static Role extractRow(ResultSet rs) throws SQLException {
        var role = new Role(rs.getLong("id"), rs.getString("role_name"), rs.getString("role_code"),rs.getString("created_at"));
        return role;
    }

    public static boolean updateRole(long id, String roleName) {
        Connection conn = DBUtil.getConnection();
        String sql = "UPDATE roles SET role_name = ? WHERE id = ?;";
        User user = null;
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setString(1, roleName);
            ps.setLong(2, id);
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean removeRole(long id) {
        Connection conn = DBUtil.getConnection();
        String sql = "DELETE FROM roles WHERE id = ?";
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setLong(1, id);
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static boolean addRole(String roleName, String roleCode) {
        Connection conn = DBUtil.getConnection();
        String role_code = DBUtil.genUniqueString();
        String sql = "INSERT INTO roles (role_name, role_code) VALUES (?, ?)";
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setString(1, roleName);
            ps.setString(2, role_code);
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int getRolesCount(){
        Connection conn = DBUtil.getConnection();
        String sql = "SELECT COUNT(*) FROM roles";
        int totalRecords=0;
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                totalRecords = rs.getInt(1);
            }
            rs.close();
            ps.close();
            return totalRecords;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    // 移除角色权限
    public static boolean removeRolePermission(int roleId) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "DELETE FROM role_permissions WHERE id = ?";
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, roleId);
            int affectedRows = ps.executeUpdate();
            return affectedRows>0;
        }
    }
    // 添加角色权限
    public static boolean addRolePermission(int roleId, int permission_id) {
        Connection conn = DBUtil.getConnection();
        String sql = "INSERT INTO role_permissions (permission_id, role_id) VALUES (?, ?)";
        try(PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, permission_id);
            ps.setInt(2, roleId);
            int affectedRows = ps.executeUpdate();
            return affectedRows>0;
        }catch(SQLException e){
            return false;
        }
    }
}
